#ifndef    _PING_H_
#define    _PING_H_

#include "my_include.h"

#define BUF_LEN 64
#define PING_REQUEST 8
#define PING_REPLY 0
#define CODE_ZERO 0

#define SOCKET_ERROR 1
#define TIMEOUT_ERROR 2
#define SUCCESS 3
#define REPLY_ERROR 4

extern uint8 req;

enum _PingStatus{
    PING_INIT = 0,
    PING_WAIT0,
    PING_SEARCH,
//    PRELIMINARY_SCREENING,
    PING_WAIT1,    
    PING_FILTRATE1,
    PING_WAIT2,    
    PING_FILTRATE2,
    PING_WAIT3,    
    PING_FILTRATE3,
    PING_WAIT4,    
    PING_FILTRATE4,
    PING_WAIT5,
    PING_LOOP,
    PING_EXIT,
    PING_ERROR,
    PING_WAIT=255,    
};

typedef struct pingmsg
{
  uint8  Type;           // 0 - Ping Reply, 8 - Ping Request
  uint8  Code;          // Always 0
  uint16 CheckSum;    // Check sum
  uint16 ID;          // Identification
  uint16 SeqNum;       // Sequence Number
  int8_t Data[BUF_LEN]; //Data[BUF_LEN];// Ping Data  : 1452 = IP RAW MTU - sizeof(Type+Code+CheckSum+ID+SeqNum)    
} PINGMSGR;

typedef struct pingrequest
{
  uint8  Type;           // 0 - Ping Reply, 8 - Ping Request
  uint8  Code;          // Always 0
  uint16 CheckSum;    // Check sum
  uint16 ID;          // Identification
  uint16 SeqNum;       // Sequence Number
  int8_t Data[512]; //Data[BUF_LEN];// Ping Data  : 1452 = IP RAW MTU - sizeof(Type+Code+CheckSum+ID+SeqNum)        
} PINGREQUEST;

typedef struct{
    bool   wait1s;   //1s        
    bool   wait3s;   //3s                
    bool   wait30s;  //30s    
    bool  ReplyReceived;    
    uint8 status;
    uint8 init_done;
  uint8 SearchDefaultIP[4];
    uint8 IPCache[255];
    uint16 CameraStartupTime;
    uint16 CameraMatchinFlag;
    uint8_t PingMode;    
    uint8_t ISPing;
}ping_t;
extern ping_t ping;

void ping_auto(uint8 s, uint8 *addr);
uint8 ping_count(uint8 s,uint8 *addr);
uint8 ping_request(uint8 s, uint8 *addr);
uint8 ping_reply(uint8 s, uint8 *addr,  uint16 rlen);
void do_ping(void);
#endif

